<?php
namespace app\controllers\api\upload;

use Fate;
// use fate\http\Request;
use app\libs\Sys;
use app\libs\LibUser;

/**
 * 所有上传的入口
 * 有这个步骤是为了以后将上传图片做成服务 独立到其他域名
 */
class PreController extends \fate\web\PureController {
    public static $PROJECT_ID_COMMON_ARTICLE = 1;

    public static $UPBASE_COMMON_ARTICLE = '/upload/article';

    public function run() {
        // $req = Request::getInstance();
        // $type = $req->getQueryString('type');
        $uid = LibUser::getLoginUid();
        if('' === $uid) {
            $this->ajaxReturn(null, 200001);
            exit;
        }

        // 关闭投稿
        if(!Sys::canPost($uid)) {
            $this->ajaxReturn(null, 500002);
            exit;
        }

        $projectId = self::$PROJECT_ID_COMMON_ARTICLE;
        $upbase = self::$UPBASE_COMMON_ARTICLE;

        // 验证标识
        $time = time();
        $token = md5(Fate::$app->securityKey . $uid . $time);

        $this->ajaxReturn([
            'server' => '/api/upload/upload',
            'params' => [
                'uid' => $uid,
                'timestamp' => $time,
                'token' => $token,
                'project_id' => $projectId,
                'upbase' => $upbase
            ]
        ]);
    }
}
